package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.FormatProductHelp;
import com.xbongbong.saas.model.FormTemplateModel;
import com.xbongbong.saas.model.OpportunityModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/05/24 14:59
 */
@Service("opportunityEditPageRenderServiceImpl")
public class OpportunityEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {

    @Resource
    private OpportunityModel opportunityModel;
    @Resource
    private FormatProductHelp formatProductHelp;
    @Resource
    private FormTemplateModel formTemplateModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
    }

    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO(formDataUpdateGetDTO.getCorpid(), formDataUpdateGetDTO.getDataId(), formDataUpdateGetDTO.getBusinessType(), false, null);
        // saas特殊格式转换
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        saasAnalysisDataDTO.setExplainMap(explainMap);
        opportunityModel.formatData(saasAnalysisDataDTO, data);
        boolean formCopy = Objects.nonNull(formDataUpdateGetDTO.getSaasSpecialParamPojo()) && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE);
        formatProductHelp.formatProduct(saasAnalysisDataDTO,data,handlerExplainVO.getHeadList(), formCopy);

        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        // 模板数据
        parseSingleRowDataVO.getResult().put(FieldTypeEnum.TEMPLATE.getAlias(), paasFormDataEntityExt.getFormId());
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form, PaasFormDataEntityExt dataEntity) throws XbbException {
        String corpid = formDataUpdateGetDTO.getCorpid();
        SaasSpecialParamPojo saasSpecialParamPojo = formDataUpdateGetDTO.getSaasSpecialParamPojo();
        boolean formCopy = Objects.nonNull(saasSpecialParamPojo) && Objects.equals(saasSpecialParamPojo.getFromCopy(), BasicConstant.ONE);
        // 编辑时saas字段显示特殊逻辑
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO(formDataUpdateGetDTO.getCorpid(), list, formCopy, saasSpecialParamPojo, dataEntity, formDataUpdateGetDTO.getLoginUser());

        opportunityModel.formatUpdateExplain(formatExplainDTO);
        // 设置模板字段
        formTemplateModel.setTemplate(corpid, form, formDataUpdateGetDTO.getBusinessType(), formDataUpdateGetDTO.getLoginUser(), list, 0);
    }

    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

    }


}
